﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using White.WebBrowser;
using White.WebBrowser.Silverlight;
using White.Core.UIItems;
using System.Windows.Automation;
using White.Core.UIItems.WindowStripControls;
using White.Core.InputDevices;
using White.Core.UIItems.ListBoxItems;
using SilverlightLesson1;
using White.Core;
using White.Core.UIItems.WindowItems;
using System.Configuration;
using White.Core.UIItems.TabItems;
using White.Core.AutomationElementSearch;
using White.Core.UIItems.MenuItems;
using White.Core.UIItems.ListViewItems;


/*Global TODO List 
 * Check for all exception messages 
 * Check for methods which can be promoted to the base class 
 * Exception messages to be corrected and can be globalized
 *  
 */

namespace RobotFramework
{
    public class WhiteLibrary : BaseLibrary
    {
        private static InternetExplorerWindow browserWindow;
        private static SilverlightDocument document;
        private static int StepCounter = 0;
        private static String CurrentCase = String.Empty;
        private static readonly string SMALL = "SMALL";
        public void Launch(String url, String Title, String Timeout)
        {
            try
            {
                browserWindow = InternetExplorer.Launch(url, Title);
                document = browserWindow.SilverlightDocument;
            }
            catch (Exception ex)
            {
                
            }
            
            int timeout = 0;
            try
            {
                timeout = Convert.ToInt32(Timeout);
            }
            catch (Exception e) { }
            timeout = timeout == 0 ? 3000 : timeout;
            System.Threading.Thread.Sleep(timeout);
        }
        
        public void Start_Test(String TestName)
        {
            if (browserWindow != null && browserWindow.SilverlightDocument != null)
            {
                document = browserWindow.SilverlightDocument;
            }
            StepCounter = 0;
            CurrentCase = TestName;
        }

        /// <summary>
        /// Method used to find a control and fires click event
        /// </summary>
        /// <param name="strControlName"></param>
        public void Click_Command_Button(String strControlName)
        {
            try
            {
                StepCounter++;
                Button button = GetControl<Button>(strControlName, document);
                button.Click();
                System.Threading.Thread.Sleep(1500);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method used to find a control and fires double click event
        /// </summary>
        /// <param name="strControlName"></param>
        public void Double_Click_Control(String strControlName)
        {
            try
            {
                StepCounter++;
                var control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)); // Search by text
                if (control == null) control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)); // Search by control's automatoin id
                if (control == null) throw new Exception(string.Format("No such control by name {0}", strControlName));
                control.DoubleClick();
                System.Threading.Thread.Sleep(1500);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method used to find a button and fires double click event
        /// </summary>
        /// <param name="strControlName"></param>
        public void Double_Click_Button(String strControlName)
        {
            try
            {
                StepCounter++;
                Button button = GetControl<Button>(strControlName,document);
                button.DoubleClick();
                System.Threading.Thread.Sleep(1500);
            }
            catch (Exception)
            {
                throw;
            }
        }


        /// <summary>
        /// Method to select an item in combobox
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="SelectedText"></param>
        public void Select_DropDown(String strControlName,string SelectedText)
        {
            try
            {
                StepCounter++;
                White.Core.UIItems.ListBoxItems.ComboBox comboBox = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as White.Core.UIItems.ListBoxItems.ComboBox;
                
                if (comboBox == null) comboBox = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as White.Core.UIItems.ListBoxItems.ComboBox;
                if (comboBox == null) throw new Exception("No Such button by name " + strControlName);

                ComboboxClick(strControlName, SelectedText, comboBox);
            }
            catch (Exception)
            {
                throw;
            }
            
        }

        /// <summary>
        /// Method to select an item in combobox
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="SelectedText"></param>
        public void Select_Combobox(String strControlName, string SelectedText)
        {
            try
            {
                StepCounter++;
                White.Core.UIItems.ListBoxItems.ComboBox comboBox = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as White.Core.UIItems.ListBoxItems.ComboBox;

                if (comboBox == null) comboBox = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as White.Core.UIItems.ListBoxItems.ComboBox;
                if (comboBox == null) throw new Exception("No Such button by name " + strControlName);

                //Get the all the listitems in List control
                AutomationElementCollection comboboxItem = comboBox.AutomationElement.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem));

                // Use expand pattern
                ExpandCollapsePattern expandPattern = (ExpandCollapsePattern)comboBox.AutomationElement.GetCurrentPattern(ExpandCollapsePattern.Pattern);

                //Expand the combobox
                expandPattern.Expand();

                System.Threading.Thread.Sleep(1000);

                // get the list item from combbox based on its selection text 
                ListItem item = comboBox.Items.Where(p => p.Text == SelectedText).FirstOrDefault();  // get the needed Item from the menu

                if (item == null) throw new Exception("Selection item is not found");

                //find the index of the list item in the combobox control 
                int index = comboBox.Items.IndexOf(item);
                //Index to set in combo box
                AutomationElement itemToSelect = comboboxItem[index];

                //Finding the pattern which need to select
                Object selectPattern = null;
                if (itemToSelect.TryGetCurrentPattern(SelectionItemPattern.Pattern, out selectPattern))
                {
                    ((SelectionItemPattern)selectPattern).Select();
                }
                // Collapse the combobox
                expandPattern.Collapse();
                System.Threading.Thread.Sleep(500);
            }
            catch (Exception)
            {
                throw;
            }

        }

        private static void ComboboxClick(String strControlName, string SelectedText, White.Core.UIItems.ListBoxItems.ComboBox comboBox)
        {
            comboBox.Click();  //opens the combobox so all items will be instansiated
            System.Threading.Thread.Sleep(500);
            ListItem item = comboBox.Items.Where(p => p.Text == SelectedText).FirstOrDefault();  // get the required Item from the menu

            if (item != null)
            {
                Mouse.Instance.Location = item.Location;
                item.Click();
                System.Threading.Thread.Sleep(1500);
            }
            else
            {
                throw new Exception(string.Format("No item {0} found in control {1} ", SelectedText, strControlName));
            }
        }

        
        /// <summary>
        /// Method used to select items in listbox/combobox
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="SelectedText"></param>
        public void Choose_From_List(String strControlName, string SelectedText)
        {
            try
            {
                StepCounter++;
                var control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName));
                if (control == null) control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName));
                if (control == null) throw new Exception("No Such control by name " + strControlName);
                if (control is ListBox)
                {
                    ListBox listbox = control as ListBox;
                    listbox.Items.Select(SelectedText);
                }
                else if (control is ComboBox)
                {
                    ComboboxClick(strControlName, SelectedText, control as ComboBox);
                }

                System.Threading.Thread.Sleep(1500);
            }
            catch (Exception)
            {
                throw;
            }

        }

        /// <summary>
        /// Method used to get all the items from a listbox
        /// </summary>
        /// <param name="strControlName"></param>
        /// <returns></returns>
        public string Fetch_All_From_List(String strControlName)
        {
            try
            {
                StepCounter++;
                ListBox listBox = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListBox;

                if (listBox == null) listBox = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListBox;
                if (listBox == null) throw new Exception("No Such control by name " + strControlName);
                StringBuilder sbItems = new StringBuilder();

                listBox.Items.ForEach(x=>
                {
                    sbItems.Append(x.Text);
                    sbItems.Append(",");
                });

                if (listBox.Items.Count > 0)
                    return sbItems.ToString().Substring(0,(sbItems.ToString().Length - 1)); // remove the last comma
                else
                    throw new Exception(string.Format("List {0} does not contain data", strControlName));

            }
            catch (Exception)
            {
                throw;
            }

        }

        /// <summary>
        /// Method to check for selection inside a listbox or a combobbox
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="SelectedText"></param>
        public void List_Control_Selection_Should_Be(String strControlName, string SelectedText)
        {
            try
            {
                StepCounter++;
                var control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName));
                if (control == null) control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName));
                if (control == null) throw new Exception("No Such control by name " + strControlName);
                if (control is ListBox)
                {
                    ListBox listbox = control as ListBox;
                    if (listbox.IsSelected(SelectedText) == false)
                        throw new Exception(string.Format("Control doesn't have the text {0} as selected", SelectedText));
                }
                else if (control is ComboBox)
                {
                    ComboBox combobox = control as ComboBox;
                    if (string.Compare(combobox.SelectedItemText, SelectedText, false) != 0)
                        throw new Exception(string.Format("Control doesn't have the text {0} as selected", SelectedText));
                }
                System.Threading.Thread.Sleep(1500);
            }
            catch (Exception)
            {
                throw;
            }

        }

        /// <summary>
        /// Method to check for no selections in combobox or listbox
        /// </summary>
        /// <param name="strControlName"></param>
        public void List_Control_Should_Have_No_Selections(String strControlName)
        {
            try
            {
                StepCounter++;
                var control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName));
                if (control == null) control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName));
                if (control == null) throw new Exception("No Such control by name " + strControlName);
                if (control is ListBox)
                {
                    ListBox listbox = control as ListBox;
                    if (string.IsNullOrEmpty(listbox.SelectedItemText)==false)
                        throw new Exception(string.Format("Control have selected text as {0}", listbox.SelectedItemText));
                }
                else if (control is ComboBox)
                {
                    ComboBox combobox = control as ComboBox;
                    if (string.IsNullOrEmpty(combobox.SelectedItemText) == false)
                        throw new Exception(string.Format("Control have selected text as {0}", combobox.SelectedItemText));
                }
                System.Threading.Thread.Sleep(1500);
            }
            catch (Exception)
            {
                throw;
            }

        }


        public string Fetch_Selected_List_Label(String strControlName)
        {
            try
            {
                StepCounter++;
                var control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName));
                if (control == null) control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName));
                if (control == null) throw new Exception("No Such control by name " + strControlName);
                if (control is ListBox)
                {
                    ListBox listbox = control as ListBox;
                    if (string.IsNullOrEmpty(listbox.SelectedItemText) == true)
                        throw new Exception("Control doesn't have selected text");
                    else
                        return listbox.SelectedItemText;
                }
                else if (control is ComboBox)
                {
                    ComboBox combobox = control as ComboBox;
                    if (string.IsNullOrEmpty(combobox.SelectedItemText) == true)
                        throw new Exception("Control doesn't have selected text");
                    else
                        return combobox.SelectedItemText;
                }
                return null;
            }
            catch (Exception)
            {
                throw;
            }

        }

        /// <summary>
        /// Fetch all selected items from combobox and list box 
        /// </summary>
        /// <param name="strControlName"></param>
        /// <returns></returns>
        public string Fetch_Selected_List_Labels(String strControlName)
        {
            try
            {
                StepCounter++;
                var control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName));
                if (control == null) control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName));
                if (control == null) throw new Exception("No Such control by name " + strControlName);
                StringBuilder sbValues = new StringBuilder();
                if (control is ListBox)
                {
                    ListBox listbox = control as ListBox;
                    foreach (ListItem item in listbox.Items)
                    {
                        if (item.IsSelected == true)
                        {
                            sbValues.Append(item.Text).Append(",");
                        }
                    }
                    return sbValues.ToString().Substring(0, (sbValues.ToString().Length - 1));
                }
                else if (control is ComboBox)
                {
                    ComboBox combobox = control as ComboBox;
                    foreach (ListItem item in combobox.Items)
                    {
                        if (item.IsSelected == true)
                        {
                            sbValues.Append(item.Text).Append(",");
                        }
                    }
                    return sbValues.ToString().Substring(0, (sbValues.ToString().Length - 1));
                }
                return null;
            }
            catch (Exception)
            {
                throw;
            }

        }

        /// <summary>
        /// Method to un check items in a listbox 
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="SelectedText"></param>
        public void DeSelect_From_List(String strControlName, string SelectedText)
        {
            try
            {
                StepCounter++;
                ListBox listBox = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListBox;

                if (listBox == null) listBox = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListBox;
                if (listBox == null) throw new Exception("No Such control by name " + strControlName);
                ListItem objItem = listBox.Items.Where(x => x.Equals(SelectedText)).FirstOrDefault();
                if(objItem.IsSelected)
                    objItem.UnCheck();
                System.Threading.Thread.Sleep(1500);
            }
            catch (Exception)
            {
                throw;
            }

        }

        /// <summary>
        /// Method to input text to a text box control
        /// </summary>
        /// <param name="FieldName"></param>
        /// <param name="Text"></param>
        public void Key_In_Text(String FieldName, String Text)
        {
            try
            {
                StepCounter++;
                if (Text == null) Text = String.Empty;
                TextBox t = GetControl<TextBox>(FieldName, document);
                if (t == null) throw new Exception(string.Format("No such control by name {0}",FieldName));
                t.Text = Text;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to click on the menu items 
        /// </summary>
        /// <param name="rootMenu"></param>
        /// <param name="menu"></param>
        public void Click_Menu(String rootMenu, String menu)
        {
            White.Core.UIItems.MenuItems.Menu mi = null;
            menu.Split(',').ToList().ForEach(x =>
            {
                MenuBar mb = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(rootMenu)) as MenuBar;
                mi = mb.MenuItemBy(White.Core.UIItems.Finders.SearchCriteria.ByText(x));
                Mouse.Instance.Location = mi.Location;
                System.Threading.Thread.Sleep(1500);
            }
            );
            if (mi != null) mi.Click();    
            
        }

        /// <summary>
        /// Method used to raise click event for any control which has that handler
        /// Tested
        /// </summary>
        /// <param name="strControlName"></param>
        public void Click_Control(string strControlName)
        {
            try
            {
                StepCounter++;
                var control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName));
                if (control == null) control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName));
                if (control == null) throw new Exception(string.Format("No such control by name {0}", strControlName));
                control.Click();
                System.Threading.Thread.Sleep(1500);
            }
            catch (Exception)
            {
                throw;
            }
        }
        
        /// <summary>
        /// method to close the browser sesssion
        /// </summary>
        public void Close_All_Web_Browsers()
        {
            try
            {
                browserWindow.Close();

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                browserWindow.Dispose();
            }
        }

        /// <summary>
        /// Method used to fetch the count of grid
        /// </summary>
        /// <param name="strControlName"></param>
        /// <returns></returns>
        public int Get_Grid_Item_Count(String strControlName)
        {
            try
            {
                StepCounter++;
                ListView test = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;
                if (test == null) test = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (test == null) throw new Exception("No Such button by name " + strControlName);
                return test.Rows.Count;
            }
            catch (Exception)
            {
                throw;
            }

        }

        /// <summary>
        /// Method to get all grid items from a column
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="intCol"></param>
        /// <returns></returns>
        public string Get_Grid_Items(String strControlName, int intCol)
        {
            try
            {
                StepCounter++;
                ListView test = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;

                if (test == null) test = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (test == null) throw new Exception("No Such button by name " + strControlName);

                StringBuilder sb = new StringBuilder(); 

                test.Rows.ForEach(x =>
                {
                    sb.Append(x.Cells[intCol].Text);
                    sb.Append(",");
                });

                sb.ToString().Substring(0, (sb.ToString().Length - 1));
                if (test.Rows.Count > 0)
                    return sb.ToString().Substring(0,(sb.ToString().Length - 1));
                else
                    throw new Exception(string.Format("Column {0} does not contain data", intCol));
            }
            catch (Exception)
            {
                throw;
            }

        }
        /// <summary>
        /// Method to check for a grid column contains a given text 
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="intCol"></param>
        /// <param name="strExpected"></param>
        public void Grid_Column_Should_Contain(String strControlName,int intCol,string strExpected)
        {
            try
            {
                StepCounter++;
                ListView test = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;

                if (test == null) test = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (test == null) throw new Exception("No Such button by name " + strControlName);

                test.Rows.ForEach(x =>
                    {
                        if (x.Cells[intCol].Text.Equals(strExpected))
                            return;
                    });
                
                throw new Exception(string.Format("Column {0} does not contain the value {1}", intCol, strExpected));
            }
            catch (Exception)
            {
                throw;
            }

        }
        /// <summary>
        /// method to check for a grid row contains a given text 
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="intRow"></param>
        /// <param name="strExpected"></param>
        public void Grid_Row_Should_Contain(String strControlName,int intRow,string strExpected)
        {
            try
            {
                StepCounter++;
                ListView test = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;

                if (test == null) test = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (test == null) throw new Exception("No Such button by name " + strControlName);
                if (test.Rows.Count == 0) throw new Exception("No rows exist");

                var automationElement = test.Rows[intRow].GetElement(White.Core.UIItems.Finders.SearchCriteria.ByText(strExpected));
                if (automationElement != null)
                    return;
                else
                    throw new Exception(string.Format("Grid {0} row {1} does not contain the value {2}", strControlName, strExpected));

            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to check whether grid cell contains a given value
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="intRow"></param>
        /// <param name="intCol"></param>
        /// <param name="strExpected"></param>
        public void Grid_Cell_Should_Contain(String strControlName, int intRow,int intCol, string strExpected)
        {
            try
            {
                StepCounter++;
                ListView test = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;

                if (test == null) test = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (test == null) throw new Exception("No Such button by name " + strControlName);

                if (test.Rows[intRow].Cells[intCol].Text.Contains(strExpected) == false)
                    throw new Exception(string.Format("Column {0} does not contain the value {1}", intCol, strExpected));
                else
                    return;
                
            }
            catch (Exception)
            {
                throw;
            }

        }

        /// <summary>
        /// Method to check for a grid should contain a given value 
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="strExpected"></param>
        public void Grid_Should_Contain(String strControlName, string strExpected)
        {
            try
            {
                StepCounter++;
                ListView test = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;

                if (test == null) test = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (test == null) throw new Exception("No Such button by name " + strControlName);
                bool FoundCell = false;
                foreach (var item in test.Rows)
	            {
		            var automationElement = item.GetElement(White.Core.UIItems.Finders.SearchCriteria.ByText(strExpected));
                    if (automationElement != null)
                    {
                        FoundCell = true;
                        break;
                    }
	            }
                if (FoundCell)
                    return;
                else
                    throw new Exception(string.Format("Grid {0} does not contain the value {1}", strControlName, strExpected));

            }
            catch (Exception)
            {
                throw;
            }

        }

        /// <summary>
        /// Method to check whether grid header contains a given text
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="strExpected"></param>
        public void Grid_Header_Should_Contain(String strControlName, string strExpected)
        {
            try
            {
                StepCounter++;
                ListView test = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;

                if (test == null) test = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (test == null) throw new Exception("No Such button by name " + strControlName);
                bool blnFound = false;
                foreach (var item in test.Header.Columns)
                {
                    if (string.Compare(item.Text, strExpected, false) == 0)
                    {
                        blnFound = true;
                        break;
                    }
                }

                if (blnFound)
                    return;
                else
                    throw new Exception(string.Format("Grid {0} Header does not contain the value {1}", strControlName, strExpected));

            }
            catch (Exception)
            {
                throw;
            }

        }

        /// <summary>
        /// method to get the combox items as a comma seperated string 
        /// Tested
        /// </summary>
        /// <param name="strControlName"></param>
        /// <returns></returns>
        public string Get_ComboBox_Items(String strControlName)
        {
            try
            {
                StepCounter++;
                ComboBox comboBox = GetControl<ComboBox>(strControlName,document);
                StringBuilder sbComboItems = new StringBuilder();
                comboBox.Items.ForEach(x => sbComboItems.Append(x.Text).Append(","));
                return sbComboItems.ToString().Substring(0,(sbComboItems.ToString().Length - 1));
            }
            catch (Exception)
            {
                throw;
            }

        }

        /// <summary>
        /// method to get the list box items as a comma seperated string
        /// Tested 
        /// </summary>
        /// <param name="strControlName"></param>
        /// <returns></returns>
        public string Get_ListBox_Items(String strControlName)
        {
            try
            {
                StepCounter++;
                ListBox listBox = GetControl<ListBox>(strControlName, document);
                StringBuilder sbComboItems = new StringBuilder();
                listBox.Items.ForEach(x => sbComboItems.Append(x.Text).Append(","));
                return sbComboItems.ToString().Substring(0, (sbComboItems.ToString().Length - 1));
            }
            catch (Exception)
            {
                throw;
            }

        }

        /// <summary>
        /// Method to click hyperlink button
        /// </summary>
        /// <param name="strControlName"></param>
        public void Click_HyperLink(String strControlName)
        {
            try
            {
                StepCounter++;
                Hyperlink link = GetControl<Hyperlink>(strControlName, document);
                link.Click();
                System.Threading.Thread.Sleep(1500);
            }
            catch (Exception)
            {
                throw;
            }
        }


        /// <summary>
        /// Method to click image control
        /// </summary>
        /// <param name="strControlName"></param>
        public void Click_Picture(String strControlName)
        {
            try
            {
                StepCounter++;
                Image image = GetControl<Image>(strControlName, document);
                image.Click();
                System.Threading.Thread.Sleep(1500);
            }
            catch (Exception)
            {
                throw ;
            }
        }

        /// <summary>
        /// Method to check for a text in the entire silverlight document
        /// </summary>
        /// <param name="strExpectedText"></param>
        public void Document_Should_Contain(string strExpectedText)
        {
            try
            {
                StepCounter++;
                if (document == null) throw new Exception("No test in progress");
                if (String.IsNullOrEmpty(strExpectedText)) throw new Exception("Control name is required. Pls specify");
                var control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strExpectedText));
                if (control == null)
                    throw new Exception(string.Format("No such control by name {0} ", strExpectedText));
                else
                    return;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to check for a text which is not available in the silverlight document
        /// </summary>
        /// <param name="strControlName"></param>
        public void Document_Should_Not_Contain(string strControlName)
        {
            try
            {
                StepCounter++;
                if (document == null) throw new Exception("No test in progress");
                if (String.IsNullOrEmpty(strControlName)) throw new Exception("Control name is required. Pls specify");
                var control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName));
                if (control == null)
                    return;
                else
                    throw new Exception(string.Format("Control {0} found ", strControlName));
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void Document_Should_Contain_Button(String strControlName)
        {
            Document_Should_Contain_Common<Button>(strControlName);
        }

        public void Document_Should_Contain_Checkbox(String strControlName)
        {
            Document_Should_Contain_Common<CheckBox>(strControlName);
        }

        public void Document_Should_Contain_Image(String strControlName)
        {
            Document_Should_Contain_Common<Image>(strControlName);
        }

        public void Document_Should_Contain_Link(String strControlName)
        {
            Document_Should_Contain_Common<Hyperlink>(strControlName);
        }

        public void Document_Should_Contain_List(String strControlName)
        {
            Document_Should_Contain_Common<ListBox>(strControlName);
        }


        public void Document_Should_Contain_Radio_Button(String strControlName)
        {
            Document_Should_Contain_Common<RadioButton>(strControlName);
        }

        public void Document_Should_Contain_TextField(String strControlName)
        {
            Document_Should_Contain_Common<TextBox>(strControlName);
        }

        private void Document_Should_Contain_Common<T>(string strControlName) where T : class
        {
            try
            {
                StepCounter++;
                T control = GetControl<T>(strControlName, document);
                if (control == null)
                    throw new Exception(string.Format("No such {0} control by name {1} ",typeof(T).Name,strControlName));
                else
                    return;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to find any element in a xaml file
        /// </summary>
        /// <param name="strControlName"></param>
        public void Document_Should_Contain_Control(string strControlName) 
        {
            try
            {
                StepCounter++;
                if (document == null) throw new Exception("No test in progress");
                if (String.IsNullOrEmpty(strControlName)) throw new Exception("Control name is required. Pls specify");
                var control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName));
                if (control == null) control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName));
                
                if (control == null)
                    throw new Exception(string.Format("No such control by name {0} ", strControlName));
                else
                    return;
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void Document_Should_Not_Contain_Common<T>(string strControlName) where T : class
        {
            try
            {
                StepCounter++;
                T control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as T;
                if (control == null) control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as T;
                if (control != null)
                    throw new Exception("Control found " + strControlName);
                else
                    return;

            }
            catch (Exception)
            {
                throw;
            }
        }


        private void Document_Should_Not_Contain_Element(string strControlName) 
        {
            try
            {
                StepCounter++;
                var control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName));
                if (control == null) control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName));
                if (control != null)
                    throw new Exception("Control found " + strControlName);
                else
                    return;

            }
            catch (Exception)
            {
                throw;
            }
        }


        public void Document_Should_Not_Contain_Button(String strControlName)
        {
            Document_Should_Not_Contain_Common<Button>(strControlName);
        }

        public void Document_Should_Not_Contain_CheckBox(String strControlName)
        {
            Document_Should_Not_Contain_Common<CheckBox>(strControlName);
        }

        public void Document_Should_Not_Contain_Image(String strControlName)
        {
            Document_Should_Not_Contain_Common<Image>(strControlName);
        }

        public void Document_Should_Not_Contain_Link(String strControlName)
        {
            Document_Should_Not_Contain_Common<Hyperlink>(strControlName);
        }

        public void Document_Should_Not_Contain_List(String strControlName)
        {
            Document_Should_Not_Contain_Common<ListBox>(strControlName);
        }

        public void Document_Should_Not_Contain_Radio_Button(String strControlName)
        {
            Document_Should_Not_Contain_Common<RadioButton>(strControlName);
        }

        public void Document_Should_Not_Contain_TextField(String strControlName)
        {
            Document_Should_Not_Contain_Common<TextBox>(strControlName);
        }

        /// <summary>
        /// Method to select checkbox 
        /// </summary>
        /// <param name="strControlName"></param>
        public void Click_CheckBox(String strControlName)
        {
            try
            {
                StepCounter++;
                CheckBox checkBox = GetControl<CheckBox>(strControlName, document);
                checkBox.Checked = true;
                System.Threading.Thread.Sleep(1500);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to un select checkbox control 
        /// </summary>
        /// <param name="strControlName"></param>
        public void Deselect_Checkbox(String strControlName)
        {
            try
            {
                StepCounter++;
                CheckBox checkBox = GetControl<CheckBox>(strControlName,document);
                checkBox.Checked = false;
                System.Threading.Thread.Sleep(1500);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to check whether a checkbox control is checked 
        /// </summary>
        /// <param name="strControlName"></param>
        public void Checkbox_Should_Be_Checked(String strControlName)
        {
            try
            {
                StepCounter++;
                CheckBox checkBox = GetControl<CheckBox>(strControlName, document);
                if(checkBox.Checked == false)
                    throw new Exception(string.Format("Checkbox {0} is unselected", strControlName));
                else
                    return;
            }
            catch (Exception)
            {
                throw;
            }
        }


        /// <summary>
        /// Method to check whether a checkbox control is un checked 
        /// </summary>
        /// <param name="strControlName"></param>
        public void Checkbox_Should_Not_Be_Checked(String strControlName)
        {
            try
            {
                StepCounter++;
                CheckBox checkBox = GetControl<CheckBox>(strControlName, document);
                if (checkBox.Checked == true)
                    throw new Exception(string.Format("Checkbox {0} is in selected state", strControlName));
                else
                    return;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to check radio button is unchecked 
        /// </summary>
        /// <param name="strControlName"></param>
        public void Option_Button_Should_Not_Be_Selected(String strControlName)
        {
            try
            {
                StepCounter++;
                RadioButton radio = GetControl<RadioButton>(strControlName, document);
                if (radio.IsSelected == true)
                    throw new Exception(string.Format("Option button {0} is in selected state", strControlName));
                else
                    return;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to check whether a radio button is checked
        /// Tested
        /// </summary>
        /// <param name="strControlName"></param>
        public void Option_Button_Should_Be_Selected(String strControlName)
        {
            try
            {
                StepCounter++;
                RadioButton radio = GetControl<RadioButton>(strControlName, document);
                if (radio.IsSelected == false)
                    throw new Exception(string.Format("Option button {0} is in unselected state", strControlName));
                else
                    return;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to key in text in a passwordbox 
        /// </summary>
        /// <param name="FieldName"></param>
        /// <param name="Text"></param>
        public void Key_In_Password(String FieldName, String Text)
        {
            Key_In_Text(FieldName, Text);
        }

        /// <summary>
        /// Method used to select a radio button 
        /// </summary>
        /// <param name="strControlName"></param>
        public void Choose_Radio_Button(String strControlName)
        {
            try
            {
                StepCounter++;
                RadioButton radio = GetControl<RadioButton>(strControlName, document);
                radio.Select();
                System.Threading.Thread.Sleep(1500);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to check whether a control text is matching with the given text
        /// Tested
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="strText"></param>
        public void Control_Text_Should_Be(string strControlName, string strText)
        {
            try
            {
                StepCounter++;
                var control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName));
                if (control == null) control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName));
                if (control == null) throw new Exception("No such control by name " + strControlName);
                if (control is TextBox)
                {
                    TextBox txtBox = control as TextBox;
                    if (string.Compare(txtBox.Text, strText, false) == 0)
                        return;
                    else
                        throw new Exception(string.Format("Control {0} text doesnot matches with the given text{1}", strControlName, strText));
                }
                else if (control is Label)
                {
                    Label label = control as Label;
                    if (string.Compare(label.Text, strText, false) == 0)
                        return;
                    else
                        throw new Exception(string.Format("Control {0} text doesnot matches with the given text{1}", strControlName, strText));
                }
                else if (control is MultilineTextBox)
                {
                    MultilineTextBox multilineTextBox = control as MultilineTextBox;
                    if (string.Compare(multilineTextBox.Text, strText, false) == 0)
                        return;
                    else
                        throw new Exception(string.Format("Control {0} text doesnot matches with the given text{1}", strControlName, strText));
                }
                
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to check whether a control text is matching with the given text
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="strText"></param>
        public void Control_Should_Contain(string strControlName, string strText)
        {
            try
            {
                StepCounter++;
                var control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName));
                if (control == null) control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName));
                if (control == null) throw new Exception("No such control by name " + strControlName);
                if (control is TextBox)
                {
                    TextBox txtBox = control as TextBox;
                    if (txtBox.Text.Contains(strText))
                        return;
                    else
                        throw new Exception(string.Format("Control {0} text doesnot matches with the given text {1}", strControlName, strText));
                }
                else if (control is Label)
                {
                    Label label = control as Label;
                    if (label.Text.Contains(strText))
                        return;
                    else
                        throw new Exception(string.Format("Control {0} text doesnot matches with the given text {1}", strControlName, strText));
                }
                else if (control is MultilineTextBox)
                {
                    MultilineTextBox multilineTextBox = control as MultilineTextBox;
                    if (multilineTextBox.Text.Contains(strText))
                        return;
                    else
                        throw new Exception(string.Format("Control {0} text doesnot matches with the given text {1}", strControlName, strText));
                }
            }
            catch (Exception)
            {
                throw;
            }
        }


        /// <summary>
        /// Method to check whether a control is disabled or not 
        /// </summary>
        /// <param name="strControlName"></param>
        public void Control_Should_Be_Disabled(String strControlName)
        {
            try
            {
                StepCounter++;
                var control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName));
                if (control == null) control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName));
                if (control == null) throw new Exception("No such link control by name " + strControlName);
                if (control.Enabled == true)
                    throw new Exception(string.Format("Control {0} is in enabled state", strControlName));
                else
                    return;

            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to set focus for a control 
        /// </summary>
        /// <param name="strControlName"></param>
        public void Set_Focus(String strControlName)
        {
            try
            {
                StepCounter++;
                var control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName));
                if (control == null) control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName));
                if (control == null) throw new Exception("No such link control by name " + strControlName);
                control.Focus();
                System.Threading.Thread.Sleep(1000);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to check whether a control is enabled or not 
        /// </summary>
        /// <param name="strControlName"></param>
        public void Control_Should_Be_Enabled(String strControlName)
        {
            try
            {
                StepCounter++;
                var control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName));
                if (control == null) control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName));
                if (control == null) throw new Exception("No such link control by name " + strControlName);
                if (control.Enabled == false)
                    throw new Exception(string.Format("Control {0} is in disabled state", strControlName));
                else
                    return;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to check for control's visiblity 
        /// </summary>
        /// <param name="strControlName"></param>
        public void Control_Should_Be_Visible(String strControlName)
        {
            try
            {
                StepCounter++;
                var control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName));
                if (control == null) control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName));
                if (control == null) throw new Exception("No such link control by name " + strControlName);
                if (control.Visible == false)
                    throw new Exception(string.Format("Control {0} is in invisible mode", strControlName));
                else
                    return;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to check a control's visibility 
        /// </summary>
        /// <param name="strControlName"></param>
        public void Control_Should_Not_Be_Visible(String strControlName)
        {

            try
            {
                StepCounter++;
                var control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName));
                if (control == null) control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName));
                if (control == null) throw new Exception("No such link control by name " + strControlName);
                if (control.Visible == true)
                    throw new Exception(string.Format("Control {0} is in visible mode", strControlName));
                else
                    return;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to capture mouse down event for a control 
        /// </summary>
        /// <param name="strControlName"></param>
        public void Mice_Down(String strControlName)
        {

            try
            {
                StepCounter++;
                var control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName));
                if (control == null) control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName));
                if (control == null) throw new Exception("No such control by name " + strControlName);
                Mouse.Instance.Location = control.Location;
                Mouse.LeftDown();
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to capture mouse up event for a control 
        /// </summary>
        /// <param name="strControlName"></param>
        public void Mice_Up(String strControlName)
        {

            try
            {
                StepCounter++;
                SetMouseLocation(strControlName);
                Mouse.LeftUp();
            }
            catch (Exception)
            {
                throw;
            }
        }


        /// <summary>
        /// Method to mouse over a control
        /// </summary>
        /// <param name="strControlName"></param>
        public void Mice_Over(String strControlName)
        {
            try
            {
                StepCounter++;
                SetMouseLocation(strControlName);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to mouse out over a control 
        /// </summary>
        /// <param name="strControlName"></param>
        public void Mouse_Out_Event(String strControlName)
        {
            try
            {
                StepCounter++;
                SetMouseLocation(strControlName);
                System.Threading.Thread.Sleep(1000);
                Mouse.Instance.Location = document.Location;
            }
            catch (Exception)
            {
                throw;
            }
        }


        /// <summary>
        /// Method to capture mouse down event for a control 
        /// </summary>
        /// <param name="strControlName"></param>
        public void Mouse_Down_On_Picture(String strControlName)
        {

            try
            {
                StepCounter++;
                SetMouseLocation(strControlName);
                Mouse.LeftDown();
                Mouse.LeftUp();

            }
            catch (Exception)
            {
                throw;
            }
        }

        private static void SetMouseLocation(String strControlName)
        {
            var control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName));
            if (control == null) control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName));
            if (control == null) throw new Exception("No such control by name " + strControlName);
            Mouse.Instance.Location = control.Location;
        }


        /// <summary>
        /// Method to capture mouse down event for a control 
        /// </summary>
        /// <param name="strControlName"></param>
        public void Mouse_Down_On_HyperLink(String strControlName)
        {

            try
            {
                StepCounter++;
                SetMouseLocation(strControlName);
                Mouse.LeftDown();
                Mouse.LeftUp();
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// method to maximize browser window
        /// Tested
        /// </summary>
        public void Maximize_Browser()
        {
            try
            {
                StepCounter++;
                browserWindow.DisplayState = White.Core.UIItems.WindowItems.DisplayState.Maximized;                
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
            
        }

        /// <summary>
        /// Method to check whether a given text contains in a text box control 
        /// </summary>
        /// <param name="FieldName"></param>
        /// <param name="Text"></param>
        public void TextBox_Should_Contain(String FieldName, String Text)
        {
            try
            {
                StepCounter++;
                if (Text == null) Text = String.Empty;
                TextBox textBox = GetControl<TextBox>(FieldName, document);
                if (textBox == null) throw new Exception(string.Format("No such control by name {0}", FieldName));
                if (textBox.Text.Contains(Text))
                    return;
                else
                    throw new Exception(string.Format("TextBox value {0} doesn't match with the given text {1}",textBox.Text,Text));
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to check whether a given text exactly matchings with a text box control 
        /// </summary>
        /// <param name="FieldName"></param>
        /// <param name="Text"></param>
        public void TextBox_Value_Should_Be(String FieldName, String Text)
        {
            try
            {
                StepCounter++;
                if (Text == null) Text = String.Empty;
                TextBox textBox = GetControl<TextBox>(FieldName, document);
                if (textBox == null) throw new Exception(string.Format("No such control by name {0}", FieldName));
                if (string.Compare(textBox.Text,Text,false)==0)
                    return;
                else
                    throw new Exception(string.Format("TextBox value {0} doesn't match with the given text {1}", textBox.Text, Text));
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to return browser title
        /// </summary>
        /// <returns></returns>
        public string Get_Browser_Title()
        {
            try
            {
                StepCounter++;
                if (browserWindow == null)
                    throw new Exception("Browser window is null");
            }
            catch (Exception)
            {
                throw;
            }
            return browserWindow.Title;

        }

        /// <summary>
        /// Method to check whether browser title matches with the given text
        /// </summary>
        /// <param name="strExpected"></param>
        public void Browser_Title_Should_Be(string strExpected)
        {
            try
            {
                StepCounter++;
                if (browserWindow == null)
                    throw new Exception("Browser window is null");

                if (String.Compare(browserWindow.Title, strExpected, false) == 0)
                    return;
                else
                    throw new Exception("Browser title is not matching with given text");
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void Navigate_To(String url, String Title, String Timeout)
        {
            Launch(url, Title, Timeout);
        }

        /// <summary>
        /// Method to check whether messge box is displayed 
        /// </summary>
        public void Prompt_Should_Be_Present()
        {
            try
            {
                StepCounter++;
                if (browserWindow == null)
                    throw new Exception("Browser window is null");
                Window Messagebox = browserWindow.MessageBox(string.Empty);
                if (Messagebox == null)
                    throw new Exception("Alert is not present");
                else
                    return;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to click on OK button in the confirm message box 
        /// </summary>
        /// <param name="strMsgTitle"></param>
        public void Click_Ok_On_Next_Confirmation()
        {
            try
            {
                StepCounter++;
                if (browserWindow == null)
                    throw new Exception("Browser window is null");
                Window Messagebox = browserWindow.MessageBox(string.Empty);
                if (Messagebox == null)
                    throw new Exception("Alert is not present");
                else
                {
                    Button button = document.Get<Button>(White.Core.UIItems.Finders.SearchCriteria.ByText("OK"));
                    if (button != null)
                    {
                        button.Click();
                        return;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to click on Cancel button in the confirm message box
        /// </summary>
        /// <param name="strMsgTitle"></param>
        public void Click_Cancel_On_Next_Confirmation()
        {
            try
            {
                StepCounter++;
                if (browserWindow == null)
                    throw new Exception("Browser window is null");
                Window Messagebox = browserWindow.MessageBox(string.Empty);
                if (Messagebox == null)
                    throw new Exception("Alert is not present");
                else
                {
                    Button button = document.Get<Button>(White.Core.UIItems.Finders.SearchCriteria.ByText("Cancel"));
                    if (button != null)
                    {
                        button.Click();
                        return;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to wait till a control is available in a silverlight document
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="intTimeout"></param>
        public void Wait_Until_Page_Contains_Control(string strControlName,int intTimeout = Util.WaitTimeout)
        {
            try
            {
                StepCounter++;
                if (document == null) throw new Exception("No test in progress");
                if (String.IsNullOrEmpty(strControlName)) throw new Exception("Control name is required. Pls specify");
                System.Threading.Thread.Sleep(intTimeout);
                var control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName));
                if (control == null) control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName));

                if (control == null)
                    throw new Exception(string.Format("No such control by name {0} ", strControlName));
                else
                    return;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to wait till a text is available in a silverlight document
        /// </summary>
        /// <param name="strExpectedText"></param>
        /// <param name="intTimeout"></param>
        public void Wait_Until_Page_Contains_Text(string strExpectedText, int intTimeout = Util.WaitTimeout)
        {
            try
            {
                StepCounter++;
                if (document == null) throw new Exception("No test in progress");
                if (String.IsNullOrEmpty(strExpectedText)) throw new Exception("Expected text is required. Pls specify");
                System.Threading.Thread.Sleep(intTimeout);
                var control = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strExpectedText));
                if (control == null)
                    throw new Exception(string.Format("No such Text {0} exists ", strExpectedText));
                else
                    return;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to click on tab control 
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="index"></param>
        public void Click_Tab(String strControlName,string index)
        {
            try
            {
                StepCounter++;
                White.Core.UIItems.TabItems.Tab tab = GetControl<White.Core.UIItems.TabItems.Tab>(strControlName, document);
                tab.Click();  
                System.Threading.Thread.Sleep(500);
                ITabPage item = tab.Pages[int.Parse(index)];

                if (item != null)
                {
                    Mouse.Instance.Location = item.Location;
                    item.Click();
                    System.Threading.Thread.Sleep(1500);
                }
                else
                {
                    throw new Exception(string.Format("No tab item found in tab{0} ", strControlName));
                }
                System.Threading.Thread.Sleep(1500);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Method to click on "click here to add new row" link in telerik grid
        /// </summary>
        /// <param name="strControlName"></param>
        public void TGrid_Add_New(String strControlName)
        {
            try
            {
                StepCounter++;
                ListView grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;
                if (grdControl == null) grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (grdControl == null) throw new Exception("No such grid by name " + strControlName);

                // fetch the click here to add new item grid view row 
                AutomationElement gridNewRow = grdControl.GetElement(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId("PART_AddNewRow"));
                // click the new row by invoke pattern
                InvokePattern invokePattern = (InvokePattern)gridNewRow.GetCurrentPattern(InvokePattern.Pattern);
                invokePattern.Invoke();
                System.Threading.Thread.Sleep(1000);
                
            }
            catch (Exception)
            {
                throw;
            }

        }
        /// <summary>
        /// Method to scroll horizontally in a grid control 
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="ScrollAmnt"></param>
        /// <param name="Type"></param>
        public void TGrid_ScrollHorizontal(String strControlName, string ScrollAmnt,string Type)
        {
            try
            {
                StepCounter++;
                ListView grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;
                if (grdControl == null) grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (grdControl == null) throw new Exception("No such grid by name " + strControlName);

                ScrollPattern pattern = grdControl.AutomationElement.GetCurrentPattern(ScrollPattern.Pattern) as ScrollPattern;
                if (string.Compare(Type, "I", true) == 0) // If type is increment
                {
                    if (string.Compare(ScrollAmnt, SMALL, true) == 0)
                        pattern.ScrollHorizontal(ScrollAmount.SmallIncrement);
                    else
                        pattern.ScrollHorizontal(ScrollAmount.LargeIncrement);
                }
                else
                {
                    if (string.Compare(ScrollAmnt, SMALL, true) == 0)
                        pattern.ScrollHorizontal(ScrollAmount.SmallDecrement);
                    else
                        pattern.ScrollHorizontal(ScrollAmount.LargeDecrement);
                }
                System.Threading.Thread.Sleep(1000);

                
            }
            catch (Exception)
            {
                throw;
            }

        }

        /// <summary>
        /// Method to scroll vertical for grid 
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="ScrollIncrement"></param>
        /// <param name="Type"></param>
        public void TGrid_ScrollVertical(String strControlName, string ScrollIncrement,string Type)
        {
            try
            {
                StepCounter++;
                ListView grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;
                if (grdControl == null) grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (grdControl == null) throw new Exception("No such grid control by name " + strControlName);

                ScrollPattern pattern = grdControl.AutomationElement.GetCurrentPattern(ScrollPattern.Pattern) as ScrollPattern;
                if (string.Compare(Type, "I", true) == 0) // If type is increment
                {
                    if (string.Compare(ScrollIncrement, SMALL, true) == 0)
                        pattern.ScrollVertical(ScrollAmount.SmallIncrement);
                    else
                        pattern.ScrollVertical(ScrollAmount.LargeIncrement);
                }
                else
                {
                    if (string.Compare(ScrollIncrement, SMALL, true) == 0)
                        pattern.ScrollVertical(ScrollAmount.SmallDecrement);
                    else
                        pattern.ScrollVertical(ScrollAmount.LargeDecrement);
                }
                System.Threading.Thread.Sleep(1000);


            }
            catch (Exception)
            {
                throw;
            }

        }

        /// <summary>
        /// Method to enter value in textcolumn of telerik grid
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="ColumnIndex"></param>
        /// <param name="strValueToEnter"></param>
        public void TGrid_Insert_TextColumn(String strControlName,string ColumnIndex,string strValueToEnter)
        {
            try
            {
                StepCounter++;
                ListView grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;
                if (grdControl == null) grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (grdControl == null) throw new Exception("No such grid by name " + strControlName);

                //AutomationElementCollection coll = row.AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "DataGridCell"));
                //Fetch the added new row from the grid
                AutomationElementCollection objLstAutomationElementCollectionRows = grdControl.AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "GridViewNewRow"));
                if (objLstAutomationElementCollectionRows != null && objLstAutomationElementCollectionRows.Count > 0)
                {
                    //Fetch the automation element for the newly added row 
                    AutomationElement objAutomationElementCells = objLstAutomationElementCollectionRows[0];
                    //Fetch all the cell collection from the newly added row 
                    AutomationElementCollection objLstAutomationElementCollectionCells = objAutomationElementCells.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "GridViewCell"));
                    // Add the automation element for cell into a collection
                    List<AutomationElement> objLstAutomationElementCells = new List<AutomationElement>();
                    foreach (AutomationElement autelt in objLstAutomationElementCollectionCells)
                    {
                        objLstAutomationElementCells.Add(autelt);
                    }

                    ListViewCell cell = AutomationHelper.GetGridCell(ColumnIndex, grdControl, objLstAutomationElementCells);
                    
                    // click the cell to make it editable
                    cell.Click();
                    //Get the editable control from the cell 
                    var box = cell.GetElement(White.Core.UIItems.Finders.SearchCriteria.ByControlType(ControlType.Edit));
                    // create a textbox control based on the automation element
                    TextBox txt = new TextBox(box, grdControl.ActionListener);
                    txt.SetValue(strValueToEnter);
                    txt.Focus();
                    Util.TabSpace(document);
                }
            }
            catch (Exception)
            {
                throw;
            }

        }
        /// <summary>
        /// Method to enter value in textcolumn while editing the grid control
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="RowIndex"></param>
        /// <param name="ColumnIndex"></param>
        /// <param name="strUpdateValue"></param>
        public void Grid_Edit_TextColumn(String strControlName, string RowIndex, string ColumnIndex, string strUpdateValue)
        {
            try
            {
                StepCounter++;
                ListView test = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;
                if (test == null) test = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (test == null) throw new Exception("No such grid by name " + strControlName);

                //AutomationElementCollection coll = row.AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "DataGridCell"));
                //Fetch all the cell collection from the row
                // if row is a telerik row, use GridViewCell
                AutomationElementCollection objLstAutomationElementCollectionCells = test.Rows[int.Parse(RowIndex)].AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "GridViewCell"));
                if (objLstAutomationElementCollectionCells.Count == 0)
                {
                    // if row is a silverlight datagrid, use DataGridCell
                    objLstAutomationElementCollectionCells = test.Rows[int.Parse(RowIndex)].AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "DataGridCell"));
                }
                // Add the automation element for cell into a collection
                List<AutomationElement> objLstAutomationElementCells = new List<AutomationElement>();
                foreach (AutomationElement autelt in objLstAutomationElementCollectionCells)
                {
                    objLstAutomationElementCells.Add(autelt);
                }

                ListViewCell cell = AutomationHelper.GetGridCell(ColumnIndex, test, objLstAutomationElementCells);

                // click the cell to make it editable
                cell.DoubleClick();
                System.Threading.Thread.Sleep(500);
                //Get the editable control from the cell 
                var box = cell.GetElement(White.Core.UIItems.Finders.SearchCriteria.ByControlType(ControlType.Edit));
                // create a textbox control based on the automation element
                TextBox txt = new TextBox(box, test.ActionListener);
                txt.SetValue(strUpdateValue);
                txt.Focus();
                Util.TabSpace(document);
                
            }
            catch (Exception)
            {
                throw;
            }

        }

        /// <summary>
        /// check the checkbox control while the row of the grid is in edit mode. This works only for Telerik grid
        /// Tested 
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="ColumnIndex"></param>
        public void TGrid_Insert_CheckBoxColumn(String strControlName, string ColumnIndex)
        {
            try
            {
                StepCounter++;
                ListView grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;
                if (grdControl == null) grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (grdControl == null) throw new Exception("No such grid by name " + strControlName);
                 
                //AutomationElementCollection coll = row.AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "DataGridCell"));
                //Fetch the added new row from the grid
                AutomationElementCollection objLstAutomationElementCollectionRows = grdControl.AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "GridViewNewRow"));
                if (objLstAutomationElementCollectionRows != null && objLstAutomationElementCollectionRows.Count > 0)
                {
                    //Fetch the automation element for the newly added row 
                    AutomationElement objAutomationElementCells = objLstAutomationElementCollectionRows[0];
                    //Fetch all the cell collection from the newly added row 
                    AutomationElementCollection objLstAutomationElementCollectionCells = objAutomationElementCells.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "GridViewCell"));
                    // Add the automation element for cell into a collection
                    List<AutomationElement> objLstAutomationElementCells = new List<AutomationElement>();
                    foreach (AutomationElement autelt in objLstAutomationElementCollectionCells)
                    {
                        objLstAutomationElementCells.Add(autelt);
                    }

                    ListViewCell cell = AutomationHelper.GetGridCell(ColumnIndex, grdControl, objLstAutomationElementCells);

                    // click the cell to make it editable
                    cell.Click();
                    //Get the editable control from the cell 
                    var box = cell.GetElement(White.Core.UIItems.Finders.SearchCriteria.ByControlType(ControlType.CheckBox));
                    // create a textbox control based on the automation element
                    CheckBox txt = new CheckBox(box, grdControl.ActionListener);
                    txt.Checked = true;
                    txt.Focus();
                    Util.TabSpace(document);
                }
            }
            catch (Exception)
            {
                throw;
            }

        }

        /// <summary>
        /// check the Checkbox control while the row of the grid is in editmode
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="RowIndex"></param>
        /// <param name="ColumnIndex"></param>
        public void Grid_Edit_CheckBoxColumn(String strControlName, string RowIndex, string ColumnIndex)
        {
            try
            {
                StepCounter++;
                ListView grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;
                if (grdControl == null) grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (grdControl == null) throw new Exception("No such grid by name " + strControlName);

                //AutomationElementCollection coll = row.AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "DataGridCell"));
                //Fetch all the cell collection from the row
                // if row is a telerik row, use GridViewCell
                AutomationElementCollection objLstAutomationElementCollectionCells = grdControl.Rows[int.Parse(RowIndex)].AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "GridViewCell"));
                if (objLstAutomationElementCollectionCells.Count == 0)
                {
                    // if row is a silverlight datagrid, use DataGridCell
                    objLstAutomationElementCollectionCells = grdControl.Rows[int.Parse(RowIndex)].AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "DataGridCell"));
                }
                // Add the automation element for cell into a collection
                List<AutomationElement> objLstAutomationElementCells = new List<AutomationElement>();
                foreach (AutomationElement autelt in objLstAutomationElementCollectionCells)
                {
                    objLstAutomationElementCells.Add(autelt);
                }

                ListViewCell cell = AutomationHelper.GetGridCell(ColumnIndex, grdControl, objLstAutomationElementCells);

                // click the cell to make it editable
                cell.Click(); // click will by default make the checkbox as checked
                //System.Threading.Thread.Sleep(1000);
                ////Get the editable control from the cell 
                var box = cell.GetElement(White.Core.UIItems.Finders.SearchCriteria.ByControlType(ControlType.CheckBox));
                //// create a textbox control based on the automation element
                //System.Threading.Thread.Sleep(1500);
                CheckBox txt = new CheckBox(box, grdControl.ActionListener);
                txt.Checked = true;
                txt.Focus();
                Util.TabSpace(document);
            }
            catch (Exception)
            {
                throw;
            }

        }

        /// <summary>
        /// check the radio button control while the row of the grid is in editmode. This works only for Telerik Grid
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="ColumnIndex"></param>
        public void TGrid_Insert_OptionButtonColumn(String strControlName, string ColumnIndex)
        {
            try
            {
                StepCounter++;
                ListView grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;
                if (grdControl == null) grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (grdControl == null) throw new Exception("No such grid by name " + strControlName);

                //AutomationElementCollection coll = row.AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "DataGridCell"));
                //Fetch the added new row from the grid
                AutomationElementCollection objLstAutomationElementCollectionRows = grdControl.AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "GridViewNewRow"));
                if (objLstAutomationElementCollectionRows != null && objLstAutomationElementCollectionRows.Count > 0)
                {
                    //Fetch the automation element for the newly added row 
                    AutomationElement objAutomationElementCells = objLstAutomationElementCollectionRows[0];
                    //Fetch all the cell collection from the newly added row 
                    AutomationElementCollection objLstAutomationElementCollectionCells = objAutomationElementCells.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "GridViewCell"));
                    // Add the automation element for cell into a collection
                    List<AutomationElement> objLstAutomationElementCells = new List<AutomationElement>();
                    foreach (AutomationElement autelt in objLstAutomationElementCollectionCells)
                    {
                        objLstAutomationElementCells.Add(autelt);
                    }

                    ListViewCell cell = AutomationHelper.GetGridCell(ColumnIndex, grdControl, objLstAutomationElementCells);

                    // click the cell to make it editable
                    cell.Click();
                    System.Threading.Thread.Sleep(1000);
                    //Get the editable control from the cell 
                    var box = cell.GetElement(White.Core.UIItems.Finders.SearchCriteria.ByControlType(ControlType.RadioButton));
                    // create a textbox control based on the automation element
                    System.Threading.Thread.Sleep(1500);
                    RadioButton txt = new RadioButton(box, grdControl.ActionListener);
                    txt.Select();
                    txt.Focus();
                    Util.TabSpace(document);
                }
            }
            catch (Exception)
            {
                throw;
            }

        }
       

        /// <summary>
        /// check the radio button control while the row of the grid is in editmode
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="RowIndex"></param>
        /// <param name="ColumnIndex"></param>
        public void Grid_Edit_OptionButtonColumn(String strControlName, string RowIndex, string ColumnIndex)
        {
            try
            {
                StepCounter++;
                ListView test = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;
                if (test == null) test = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (test == null) throw new Exception("No such grid by name " + strControlName);

                //AutomationElementCollection coll = row.AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "DataGridCell"));
                //Fetch all the cell collection from the row
                // if row is a telerik row, use GridViewCell
                AutomationElementCollection objLstAutomationElementCollectionCells = test.Rows[int.Parse(RowIndex)].AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "GridViewCell"));
                if (objLstAutomationElementCollectionCells.Count == 0)
                {
                    // if row is a silverlight datagrid, use DataGridCell
                    objLstAutomationElementCollectionCells = test.Rows[int.Parse(RowIndex)].AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "DataGridCell"));
                }
                // Add the automation element for cell into a collection
                List<AutomationElement> objLstAutomationElementCells = new List<AutomationElement>();
                foreach (AutomationElement autelt in objLstAutomationElementCollectionCells)
                {
                    objLstAutomationElementCells.Add(autelt);
                }

                ListViewCell cell = AutomationHelper.GetGridCell(ColumnIndex, test, objLstAutomationElementCells);

                // click the cell to make it editable
                cell.DoubleClick();
                System.Threading.Thread.Sleep(1000);
                //Get the editable control from the cell 
                var box = cell.GetElement(White.Core.UIItems.Finders.SearchCriteria.ByControlType(ControlType.RadioButton));
                // create a textbox control based on the automation element
                System.Threading.Thread.Sleep(1500);
                RadioButton txt = new RadioButton(box, test.ActionListener);
                txt.Select();
                txt.Focus();
                Util.TabSpace(document);

            }
            catch (Exception)
            {
                throw;
            }

        }

        /// <summary>
        /// Method to insert value in Telerik Grid Link column
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="ColumnIndex"></param>
        public void TGrid_Insert_LinkColumn(String strControlName, string ColumnIndex)
        {
            try
            {
                StepCounter++;
                ListView test = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;
                if (test == null) test = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (test == null) throw new Exception("No such grid by name " + strControlName);

                //AutomationElementCollection coll = row.AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "DataGridCell"));
                //Fetch the added new row from the grid
                AutomationElementCollection objLstAutomationElementCollectionRows = test.AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "GridViewNewRow"));
                if (objLstAutomationElementCollectionRows != null && objLstAutomationElementCollectionRows.Count > 0)
                {
                    //Fetch the automation element for the newly added row 
                    AutomationElement objAutomationElementCells = objLstAutomationElementCollectionRows[0];
                    //Fetch all the cell collection from the newly added row 
                    AutomationElementCollection objLstAutomationElementCollectionCells = objAutomationElementCells.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "GridViewCell"));
                    // Add the automation element for cell into a collection
                    List<AutomationElement> objLstAutomationElementCells = new List<AutomationElement>();
                    foreach (AutomationElement autelt in objLstAutomationElementCollectionCells)
                    {
                        objLstAutomationElementCells.Add(autelt);
                    }

                    ListViewCell cell = AutomationHelper.GetGridCell(ColumnIndex, test, objLstAutomationElementCells);

                    // click the cell to make it editable
                    cell.Click();
                    //Get the editable control from the cell 
                    var box = cell.GetElement(White.Core.UIItems.Finders.SearchCriteria.ByControlType(ControlType.Hyperlink));
                    // create a hyperlink control based on the automation element
                    Hyperlink link = new Hyperlink(box, test.ActionListener);
                    link.Click();
                    Util.TabSpace(document);
                }
            }
            catch (Exception)
            {
                throw;
            }

        }

        public void Grid_Edit_LinkColumn(String strControlName, string RowIndex, string ColumnIndex)
        {
            try
            {
                StepCounter++;
                ListView test = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;
                if (test == null) test = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (test == null) throw new Exception("No such grid by name " + strControlName);

                //AutomationElementCollection coll = row.AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "DataGridCell"));
                //Fetch all the cell collection from the row
                // if row is a telerik row, use GridViewCell
                AutomationElementCollection objLstAutomationElementCollectionCells = test.Rows[int.Parse(RowIndex)].AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "GridViewCell"));
                if (objLstAutomationElementCollectionCells.Count == 0)
                {
                    // if row is a silverlight datagrid, use DataGridCell
                    objLstAutomationElementCollectionCells = test.Rows[int.Parse(RowIndex)].AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "DataGridCell"));
                }
                // Add the automation element for cell into a c ollection
                List<AutomationElement> objLstAutomationElementCells = new List<AutomationElement>();
                foreach (AutomationElement autelt in objLstAutomationElementCollectionCells)
                {
                    objLstAutomationElementCells.Add(autelt);
                }

                ListViewCell cell = AutomationHelper.GetGridCell(ColumnIndex, test, objLstAutomationElementCells);
                //Get the editable control from the cell 
                var box = cell.GetElement(White.Core.UIItems.Finders.SearchCriteria.ByControlType(ControlType.Hyperlink));
                // create a hyperlink control based on the automation element
                Hyperlink link = new Hyperlink(box, test.ActionListener);
                link.Click();
                Util.TabSpace(document);

            }
            catch (Exception)
            {
                throw;
            }

        }

        public void TGrid_Insert_DateColumn(String strControlName, string ColumnIndex,string strDate)
        {
            try
            {
                StepCounter++;
                ListView grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;
                if (grdControl == null) grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (grdControl == null) throw new Exception("No such grid control by name " + strControlName);

                //AutomationElementCollection coll = row.AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "DataGridCell"));
                //Fetch the added new row from the grid
                AutomationElementCollection objLstAutomationElementCollectionRows = grdControl.AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "GridViewNewRow"));
                if (objLstAutomationElementCollectionRows != null && objLstAutomationElementCollectionRows.Count > 0)
                {
                    //Fetch the automation element for the newly added row 
                    AutomationElement objAutomationElementCells = objLstAutomationElementCollectionRows[0];
                    //Fetch all the cell collection from the newly added row 
                    AutomationElementCollection objLstAutomationElementCollectionCells = objAutomationElementCells.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "GridViewCell"));
                    // Add the automation element for cell into a collection
                    List<AutomationElement> objLstAutomationElementCells = new List<AutomationElement>();
                    foreach (AutomationElement autelt in objLstAutomationElementCollectionCells)
                    {
                        objLstAutomationElementCells.Add(autelt);
                    }

                    ListViewCell cell = AutomationHelper.GetGridCell(ColumnIndex, grdControl, objLstAutomationElementCells);

                    // click the cell to make it editable
                    cell.Click();
                    //Get the editable control from the cell 
                    var box = cell.GetElement(White.Core.UIItems.Finders.SearchCriteria.ByControlType(ControlType.Edit));
                    // create a textbox control based on the automation element
                    TextBox txtDate = new TextBox(box, grdControl.ActionListener);
                    //txtDate.Text = strDate;
                    txtDate.SetValue(strDate);
                    txtDate.Focus();
                    //DateTimePicker dtp = new DateTimePicker(box, test.ActionListener);
                    //dtp.SetDate(DateTime.Now, DateFormat.DayMonthYear);
                    //dtp.Focus();
                    Util.TabSpace(document);
                }
            }
            catch (Exception)
            {
                throw;
            }

        }

        public void Grid_Edit_DateColumn(String strControlName, string RowIndex,string ColumnIndex, string strDate)
        {
            try
            {
                StepCounter++;
                ListView test = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;
                if (test == null) test = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (test == null) throw new Exception("No such grid by name " + strControlName);

                //AutomationElementCollection coll = row.AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "DataGridCell"));
                //Fetch all the cell collection from the row
                // if row is a telerik row, use GridViewCell
                AutomationElementCollection objLstAutomationElementCollectionCells = test.Rows[int.Parse(RowIndex)].AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "GridViewCell"));
                if (objLstAutomationElementCollectionCells.Count == 0)
                {
                    // if row is a silverlight datagrid, use DataGridCell
                    objLstAutomationElementCollectionCells = test.Rows[int.Parse(RowIndex)].AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "DataGridCell"));
                }
                // Add the automation element for cell into a collection
                List<AutomationElement> objLstAutomationElementCells = new List<AutomationElement>();
                foreach (AutomationElement autelt in objLstAutomationElementCollectionCells)
                {
                    objLstAutomationElementCells.Add(autelt);
                }

                ListViewCell cell = AutomationHelper.GetGridCell(ColumnIndex, test, objLstAutomationElementCells);

                // click the cell to make it editable
                cell.DoubleClick();

                System.Threading.Thread.Sleep(1000);
                //Get the editable control from the cell 
                var box = cell.GetElement(White.Core.UIItems.Finders.SearchCriteria.ByControlType(ControlType.Edit));
                // create a textbox control based on the automation element
                TextBox txtDate = new TextBox(box, test.ActionListener);
                txtDate.Text = strDate;
                txtDate.Focus();
                //DateTimePicker dtp = new DateTimePicker(box, test.ActionListener);
                //dtp.SetDate(DateTime.Now, DateFormat.DayMonthYear);
                //dtp.Focus();
                Util.TabSpace(document);
            }
            catch (Exception)
            {
                throw;
            }

        }

        /// <summary>
        /// Method to select a row in grid control 
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="RowIndex"></param>
        public void Grid_Select_Row(String strControlName, string RowIndex)
        {
            try
            {
                StepCounter++;
                ListView grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;
                if (grdControl == null) grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (grdControl == null) throw new Exception("No such grid control by name " + strControlName);

                ListViewRow lstViewRow = grdControl.Rows[int.Parse(RowIndex)];
                while (lstViewRow.Enabled == false)
                {
                    ScrollPattern pattern = grdControl.AutomationElement.GetCurrentPattern(ScrollPattern.Pattern) as ScrollPattern;
                    pattern.ScrollVertical(ScrollAmount.SmallIncrement);
                }
                // Use selection pattern to select a row in grid  as lstviewRow.Select or lstViewRow.click is not working
                SelectionItemPattern selectPattern = (SelectionItemPattern)lstViewRow.AutomationElement.GetCurrentPattern(SelectionItemPattern.Pattern);
                selectPattern.Select();
                System.Threading.Thread.Sleep(1000);
            }
            catch (Exception)
            {
                throw;
            }

        }

        public void Grid_Press_Special_Key(string strControlName,string rowIndex, string ColumnName,string strSpecialKey)
        {
            try
            {
                StepCounter++;
                ListView grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;
                if (grdControl == null) grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (grdControl == null) throw new Exception("No such grid by name " + strControlName);

                //AutomationElementCollection coll = row.AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "DataGridCell"));
                //Fetch all the cell collection from the row
                // if row is a telerik row, use GridViewCell
                AutomationElementCollection objLstAutomationElementCollectionCells = grdControl.Rows[int.Parse(rowIndex)].AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "GridViewCell"));
                if (objLstAutomationElementCollectionCells.Count == 0)
                {
                    // if row is a silverlight datagrid, use DataGridCell
                    objLstAutomationElementCollectionCells = grdControl.Rows[int.Parse(rowIndex)].AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "DataGridCell"));
                }
                // Add the automation element for cell into a collection
                List<AutomationElement> objLstAutomationElementCells = new List<AutomationElement>();
                foreach (AutomationElement autelt in objLstAutomationElementCollectionCells)
                {
                    objLstAutomationElementCells.Add(autelt);
                }

                ListViewCell cell = AutomationHelper.GetGridCell(ColumnName, grdControl, objLstAutomationElementCells);

                // click the cell to make it editable
                cell.Click();
                AttachedKeyboard keyboard = document.Keyboard;
                foreach (White.Core.WindowsAPI.KeyboardInput.SpecialKeys item in Enum.GetValues(typeof(White.Core.WindowsAPI.KeyboardInput.SpecialKeys)))
                {
                    if (string.Compare(item.ToString(), strSpecialKey, true) == 0)
                    {
                        keyboard.PressSpecialKey(item);
                        break;
                    }
                }
                System.Threading.Thread.Sleep(1000);
            }
            catch (Exception)
            {
                throw;
            }

        }

        public void Press_Special_Key(String strSpecialKey)
        {
            try
            {
                StepCounter++;
                AttachedKeyboard keyboard = document.Keyboard;
                foreach (White.Core.WindowsAPI.KeyboardInput.SpecialKeys item in Enum.GetValues(typeof(White.Core.WindowsAPI.KeyboardInput.SpecialKeys)))
                {
                    if (string.Compare(item.ToString(), strSpecialKey, true) == 0)
                    {
                        keyboard.PressSpecialKey(item);
                        break;
                    }
                }
                System.Threading.Thread.Sleep(1000);
            }
            catch (Exception)
            {
                throw;
            }

        }

        public void Grid_DoubleClick_Row(String strControlName, string RowIndex)
        {
            try
            {
                StepCounter++;
                ListView grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;
                if (grdControl == null) grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (grdControl == null) throw new Exception("No such grid control by name " + strControlName);

                ListViewRow lstViewRow = grdControl.Rows[int.Parse(RowIndex)];
                // Use selection pattern to select a row in grid  as lstviewRow.Select or lstViewRow.click is not working
                lstViewRow.DoubleClick();
                System.Threading.Thread.Sleep(1000);
            }
            catch (Exception)
            {
                throw;
            }

        }

        public void TGrid_Insert_ComboBoxColumn(String strControlName, string ColumnIndex, string strValueToEnter)
        {
            try
            {
                StepCounter++;
                ListView grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;
                if (grdControl == null) grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (grdControl == null) throw new Exception("No such grid by name " + strControlName);

                //AutomationElementCollection coll = row.AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "DataGridCell"));
                //Fetch the added new row from the grid
                AutomationElementCollection objLstAutomationElementCollectionRows = grdControl.AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "GridViewNewRow"));
                if (objLstAutomationElementCollectionRows != null && objLstAutomationElementCollectionRows.Count > 0)
                {
                    //Fetch the automation element for the newly added row 
                    AutomationElement objAutomationElementCells = objLstAutomationElementCollectionRows[0];
                    //Fetch all the cell collection from the newly added row 
                    AutomationElementCollection objLstAutomationElementCollectionCells = objAutomationElementCells.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "GridViewCell"));
                    // Add the automation element for cell into a collection
                    List<AutomationElement> objLstAutomationElementCells = new List<AutomationElement>();
                    foreach (AutomationElement autelt in objLstAutomationElementCollectionCells)
                    {
                        objLstAutomationElementCells.Add(autelt);
                    }

                    ListViewCell cell = AutomationHelper.GetGridCell(ColumnIndex, grdControl, objLstAutomationElementCells);
                    
                    // click the cell to make it editable
                    cell.Click();
                    System.Threading.Thread.Sleep(1000);
                    //Get the editable control from the cell 
                    var box = cell.GetElement(White.Core.UIItems.Finders.SearchCriteria.ByControlType(ControlType.ComboBox));
                    // create a textbox control based on the automation element
                    
                    ComboBox comboBox = new ComboBox(box, grdControl.ActionListener);

                    //Get the all the listitems in List control
                    AutomationElementCollection comboboxItem =comboBox.AutomationElement.FindAll(TreeScope.Children,new PropertyCondition(AutomationElement.ControlTypeProperty,ControlType.ListItem));

                    //Expand the combobox
                    ExpandCollapsePattern expandPattern =(ExpandCollapsePattern)comboBox.AutomationElement.GetCurrentPattern(ExpandCollapsePattern.Pattern);

                    expandPattern.Expand();

                    System.Threading.Thread.Sleep(1000);

                    // get the list item from combbox based on its selection text 
                    ListItem item = comboBox.Items.Where(p => p.Text == strValueToEnter).FirstOrDefault();  // get the needed Item from the menu

                    if (item == null) throw new Exception("Selection item is not found");

                    while (item.Enabled == false)
                    {
                        ScrollPattern pattern = comboBox.AutomationElement.GetCurrentPattern(ScrollPattern.Pattern) as ScrollPattern;
                        pattern.ScrollVertical(ScrollAmount.SmallIncrement);
                    }

                    //find the index of the list item in the combobox control 
                    //int index = comboBox.Items.IndexOf(item);
                    int index = comboBox.Items.FindIndex(x => x.Text == item.Text);
                    //Index to set in combo box
                    AutomationElement itemToSelect = comboboxItem[index];

                    //Finding the pattern which need to select
                    Object selectPattern = null;
                    if (itemToSelect.TryGetCurrentPattern(SelectionItemPattern.Pattern, out selectPattern))
                    {
                        ((SelectionItemPattern)selectPattern).Select();
                    }

                    expandPattern.Collapse();
                    
                    Util.TabSpace(document);
                    
                }
            }
            catch (Exception)
            {
                throw;
            }

        }

        public void Grid_Edit_ComboBoxColumn(String strControlName, string RowIndex,string ColumnIndex, string strValueToEnter)
        {
            try
            {
                StepCounter++;
                ListView grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByText(strControlName)) as ListView;
                if (grdControl == null) grdControl = document.Get(White.Core.UIItems.Finders.SearchCriteria.ByAutomationId(strControlName)) as ListView;
                if (grdControl == null) throw new Exception("No such grid by name " + strControlName);

                //AutomationElementCollection coll = row.AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "DataGridCell"));
                //Fetch all the cell collection from the row
                // if row is a telerik row, use GridViewCell
                AutomationElementCollection objLstAutomationElementCollectionCells = grdControl.Rows[int.Parse(RowIndex)].AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "GridViewCell"));
                if (objLstAutomationElementCollectionCells.Count == 0)
                {
                    // if row is a silverlight datagrid, use DataGridCell
                    objLstAutomationElementCollectionCells = grdControl.Rows[int.Parse(RowIndex)].AutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "DataGridCell"));
                }
                // Add the automation element for cell into a collection
                List<AutomationElement> objLstAutomationElementCells = new List<AutomationElement>();
                foreach (AutomationElement autelt in objLstAutomationElementCollectionCells)
                {
                    objLstAutomationElementCells.Add(autelt);
                }

                ListViewCell cell = AutomationHelper.GetGridCell(ColumnIndex, grdControl, objLstAutomationElementCells);

                // click the cell to make it editable
                cell.Click();
                System.Threading.Thread.Sleep(1000);
                //Get the editable control from the cell 
                var box = cell.GetElement(White.Core.UIItems.Finders.SearchCriteria.ByControlType(ControlType.ComboBox));
                // create a textbox control based on the automation element

                ComboBox comboBox = new ComboBox(box, grdControl.ActionListener);

                //Get the all the listitems in List control
                AutomationElementCollection comboboxItem = comboBox.AutomationElement.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem));

                //Expand the combobox
                ExpandCollapsePattern expandPattern = (ExpandCollapsePattern)comboBox.AutomationElement.GetCurrentPattern(ExpandCollapsePattern.Pattern);

                expandPattern.Expand();

                System.Threading.Thread.Sleep(1000);

                // get the list item from combbox based on its selection text 
                ListItem item = comboBox.Items.Where(p => p.Text == strValueToEnter).FirstOrDefault();  // get the needed Item from the menu

                if (item == null) throw new Exception("Selection item is not found");

                //find the index of the list item in the combobox control 
                int index = comboBox.Items.IndexOf(item);
                //Index to set in combo box
                AutomationElement itemToSelect = comboboxItem[index];

                //Finding the pattern which need to select
                Object selectPattern = null;
                if (itemToSelect.TryGetCurrentPattern(SelectionItemPattern.Pattern, out selectPattern))
                {
                    ((SelectionItemPattern)selectPattern).Select();
                }

                expandPattern.Collapse();

                Util.TabSpace(document);
            }
            catch (Exception)
            {
                throw;
            }

        }

        /// <summary>
        /// Method used to select the date in the datetimepicker control
        /// </summary>
        /// <param name="strControlName"></param>
        /// <param name="strDate"></param>
        public void Select_Date(String strControlName,string strDate)
        {
            try
            {
                StepCounter++;
                DateTimePicker dtPicker = GetControl<DateTimePicker>(strControlName, document);
                string[] arrDate = strDate.Split(',');
                dtPicker.Date = new DateTime(int.Parse(arrDate[0]), int.Parse(arrDate[1]), int.Parse(arrDate[2]));
                System.Threading.Thread.Sleep(1500);
            }
            catch (Exception)
            {
                throw;
            }
        }

        #region File Save\Open Dialog
        /// <summary>
        /// Used to choose a file and save it in the specified location 
        /// Path should contain \\ e.g. C:\\Users\\username\\Desktop\\sample.txt
        /// Tested
        /// </summary>
        /// <param name="strPath"></param>
        public void Save_File(string strPath)
        {
            try
            {
                StepCounter++;
                var modalwindow = browserWindow.MdiChild(White.Core.UIItems.Finders.SearchCriteria.ByControlType(ControlType.Window));
                IUIItem[] items = browserWindow.GetMultiple(White.Core.UIItems.Finders.SearchCriteria.ByControlType(ControlType.Edit));
                if (items.Count() > 0)
                {
                    TextBox txt = items[0] as TextBox;
                    if (txt != null)
                    {
                        txt.Text = strPath;
                        Button SaveAs = modalwindow.Get(White.Core.UIItems.Finders.SearchCriteria.ByText("Save")) as Button;
                        SaveAs.Click();
                    }
                }
                System.Threading.Thread.Sleep(1500);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        /// <summary>
        /// Used to open a file 
        /// Path should contain \\ e.g. C:\\Users\\username\\Desktop\\sample.txt
        /// Tested
        /// </summary>
        /// <param name="strPath"></param>
        public void Open_File(string strPath)
        {
            try
            {
                StepCounter++;
                var modalwindow = browserWindow.MdiChild(White.Core.UIItems.Finders.SearchCriteria.ByControlType(ControlType.Window));
                IUIItem[] items = browserWindow.GetMultiple(White.Core.UIItems.Finders.SearchCriteria.ByControlType(ControlType.Edit));
                if (items.Count() > 0)
                {
                    TextBox txt = items[0] as TextBox;
                    if (txt != null)
                    {
                        txt.Text = strPath;
                        Button SaveAs = modalwindow.Get(White.Core.UIItems.Finders.SearchCriteria.ByText("Open")) as Button;
                        SaveAs.Click();
                    }
                }
                System.Threading.Thread.Sleep(1500);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        #endregion

    }
}
